<!DOCTYPE html>
<html lang="ru">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<!--script src='./dist/sql-wasm-debug.js'></script-->
<script src='sql-asm-debug.js'></script>
<script>

  // Путь к файлу .wasm
  //config = {
  //  locateFile: (filename, prefix) => {
  //    console.log(`prefix is : ${prefix}`);
  //    return `./dist/${filename}`;
  //  }
  //}
  //initSqlJs(config).then(function (SQL) {

  // Асинхронная загрузка библиотеки sql.js через промис
  /*
  initSqlJs().then(function (SQL) {

    var db = new SQL.Database();

    db.run("CREATE TABLE test (col1, col2);");
    db.run("INSERT INTO test VALUES (?,?), (?,?)", [1, 111, 2, 222]);

    var stmt = db.prepare("SELECT * FROM test WHERE col1 BETWEEN $start AND $end");
    stmt.getAsObject({ $start: 1, $end: 1 }); // {col1:1, col2:111}

    stmt.bind({ $start: 1, $end: 2 });
    while (stmt.step()) {
      var row = stmt.getAsObject();
      console.log('Here is a row: ' + JSON.stringify(row));
    }
  });
  */

  // Асинхронная загрузка slq.js через async
  (async ()=> {
  
    const initSqlJs = window.initSqlJs;
    const SQL = await initSqlJs();
    const db = new SQL.Database();
  
    let sqlstr = `CREATE TABLE hello (a int, b char, c char);
      INSERT INTO hello VALUES (0, 'hello', 'line0');
      INSERT INTO hello VALUES (1, 'world', 'line1');`;
  
    await db.run(sqlstr);
   
    const stmt = await db.prepare("SELECT * FROM hello WHERE a=$a AND b=$b");
    const result = stmt.getAsObject({ $a: 1, $b: 'world' });
  
    console.log(result);

  })();
  
</script>

<body>
  Output is in Javascript console
</body>
</html>
